From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Fri, 30 Aug 2024 23:11:09 +0000 (-0600) Subject: normalize waypoint position. (#1331) X-Git-Tag: archive/raspbian/1.10.0+ds-2+rpi1~1^2~12^2^2~60 X-Git-Url: https://dgit.raspbian.org/%22http:/www.example.com/cgi/%22https://%22Program/%22http:/www.example.com/cgi/%22https:/%22Program?a=commitdiff_plain;h=314d98779ba1c3967bfa95be2c66a69b7a164bb5;p=gpsbabel.git normalize waypoint position. (#1331) --- diff --git a/defs.h b/defs.h index d50e49429..2380b89e4 100644 --- a/defs.h +++ b/defs.h @@ -357,6 +357,7 @@ public: void SetCreationTime(qint64 t, qint64 ms = 0); Geocache* AllocGCData(); int EmptyGCData() const; + void NormalizePosition(); PositionDeg position() const {return PositionDeg(latitude, longitude);} void SetPosition(const PositionDeg& pos) { diff --git a/waypt.cc b/waypt.cc index b26455511..191cdc841 100644 --- a/waypt.cc +++ b/waypt.cc @@ -560,31 +560,53 @@ Waypoint::EmptyGCData() const return (gc_data == &Waypoint::empty_gc_data); } -void -WaypointList::waypt_add(Waypoint* wpt) -{ - double lat_orig = wpt->latitude; - double lon_orig = wpt->longitude; - append(wpt); +void Waypoint::NormalizePosition() +{ + double lat_orig = this->latitude; + double lon_orig = this->longitude; + + if (this->latitude < -90.0 || this->latitude > 90.0) { + bool fliplon = false; + this->latitude = remainder(this->latitude, 360.0); // -180 <= this->latitude <= 180 + if (this->latitude < -90.0) { + this->latitude = -180.0 - this->latitude; + fliplon = true; + } else if (this->latitude > 90.0) { + this->latitude = 180.0 - this->latitude; + fliplon = true; + } - if (wpt->latitude < -90) { - wpt->latitude += 180; - } else if (wpt->latitude > +90) { - wpt->latitude -= 180; + if (fliplon) { + if (this->longitude < 0.0) { + this->longitude += 180.0; + } else { + this->longitude -= 180.0; + } + } } - if (wpt->longitude < -180) { - wpt->longitude += 360; - } else if (wpt->longitude > +180) { - wpt->longitude -= 360; + + if (this->longitude < -180.0 || this->longitude >= 180.0) { + this->longitude = remainder(this->longitude, 360.0); // -180 <= this->longitude <= 180 + if (this->longitude == 180.0) { + this->longitude = -180.0; + } } - if ((wpt->latitude < -90) || (wpt->latitude > 90.0)) - fatal(FatalMsg() << wpt->session->name + if ((this->latitude < -90) || (this->latitude > 90.0)) + fatal(FatalMsg() << this->session->name << "Invalid latitude" << lat_orig << "in waypoint" - << wpt->shortname); - if ((wpt->longitude < -180) || (wpt->longitude > 180.0)) + << this->shortname); + if ((this->longitude < -180) || (this->longitude > 180.0)) fatal(FatalMsg() << "Invalid longitude" << lon_orig << "in waypoint" - << wpt->shortname); + << this->shortname); +} + +void +WaypointList::waypt_add(Waypoint* wpt) +{ + append(wpt); + + wpt->NormalizePosition(); /* * Some input may not have one or more of these types so we @@ -621,6 +643,8 @@ WaypointList::add_rte_waypt(int waypt_ct, Waypoint* wpt, bool synth, QStringView { append(wpt); + wpt->NormalizePosition(); + if (synth && wpt->shortname.isEmpty()) { wpt->shortname = QStringLiteral("%1%2").arg(namepart).arg(waypt_ct, number_digits, 10, QChar('0')); wpt->wpt_flags.shortname_is_synthetic = 1;